package org.gdstash.combobox;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
 
public class ComboBoxTooltipExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JComboBox Tooltip Example");
        String[] items = {"选项1", "选项2", "选项3"};
        final JComboBox<String> comboBox = new JComboBox<>(items);
 
        // 为comboBox添加ItemListener来监听选项变化
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    // 设置tooltip文本
                    comboBox.setToolTipText("你选择了: " + e.getItem());
                }
            }
        });
 
        frame.add(comboBox);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}